/*
 * @Descripttion:
 * @LastEditors: xzh
 * @LastEditTime: 2023-11-04 22:11:19
 */

import { object_Inf } from "@/utils/typings/index.type";
import Common_Methods from "../common";

/*
 * @Descripttion:格式化工具方法类
 * @LastEditors: xzh
 * @LastEditTime: 2022-05-25 20:21:52
 */
export default class Format_Methods extends Common_Methods {
  /**
   * @Author: xzh
   * @Descripttion: 格式化数据成树结构
   * @Param:
   * @param {Array} data
   * @param {object_Inf} props
   */
  static formatToTree(
    data: Array<object_Inf>,
    props: object_Inf = { id: "id", parentId: "parentId", children: "children" }
  ) {
    //过滤停用&&删除的对象
    data = this.filterDataArr(data);
    data.map((x) => x[props.id]);
    //获取根节点
    let treeData = data.filter(
        (x) =>
          !x[props.parentId] ||
          !data
            .map((y) => y[props.id])
            .includes(!x[props.parentId] || x[props.parentId]) ||
          x[props.parentId] == this.emptyGuid()
      ),
      //第一个子节点
      firstChildNode = {};
    treeData.forEach((root) => {
      let {
        children,
        firstChildNode: _firstChildNode,
        hasChildren,
      } = this.formatChildTree({
        item: root,
        data,
        props,
        firstChildNode,
      });
      root[props.children] = children;
      root.hasChildren = hasChildren;
      firstChildNode = _firstChildNode;
    });
    return { treeData, firstChildNode };
  }
  /**
   * @Author: xzh
   * @Descripttion:
   * @Param:
   */
  static formatChildTree(param: object_Inf) {
    let { item, data, props, firstChildNode } = param;
    //获取节点对应子节点
    let children = data.filter(
      (x: object_Inf) => x[props.parentId] == item[props.id]
    );
    //记录第一个子节点
    children.length == 0 &&
      Object.keys(firstChildNode).length == 0 &&
      (firstChildNode = item);

    children.forEach((_item: object_Inf) => {
      //拼接子节点
      let {
        children,
        firstChildNode: _firstChildNode,
        hasChildren,
      } = this.formatChildTree({
        item: _item,
        data,
        props,
        firstChildNode,
      });
      _item[props.children] = children;
      _item.hasChildren = hasChildren;
      firstChildNode = _firstChildNode;
    });
    return { children, firstChildNode, hasChildren: children.length == 0 };
  }
  /**
   * @Author: xzh
   * @Descripttion: 过滤对象
   * @Param:
   * @param {Array} dataArr
   */
  static filterDataArr(dataArr: Array<object_Inf>) {
    return (dataArr || []).filter(
      (x: object_Inf) => x.status != false && x.isDelete != true
    );
  }

  /**
   * @Author: xzh
   * @Descripttion: 数组差异格式化
   * @Param:
   */
  static dataArrDiffFormat(
    sourceDataArr: Array<object_Inf>,
    dataArr: Array<object_Inf>,
    key: string = "id"
  ) {
    //排除空数据
    dataArr = dataArr.filter(
      (x: object_Inf) =>
        Object.keys(x).filter((key: string) => !["rowId"].includes(key))
          .length > 0
    );
    //新增数据->源数组中不存在
    let addRelations = dataArr.filter(
      (x: object_Inf) =>
        !sourceDataArr.map((y: object_Inf) => y[key]).includes(x[key])
    );
    //修改数据->源数组中存在&&id不为空
    let eidtRelations = dataArr.filter(
      (x: object_Inf) =>
        !!sourceDataArr.map((y: object_Inf) => y[key]).includes(x[key]) &&
        !!x[key]
    );
    //删除数据->源数据存在,现数组不存在
    let deleteRelations = sourceDataArr
      .filter(
        (x: object_Inf) =>
          !dataArr.map((y: object_Inf) => y[key]).includes(x[key])
      )
      .map((x: object_Inf) => x.relId);

    return {
      addRelations,
      eidtRelations,
      deleteRelations,
    };
  }
}
